package com.skyline.attraction.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.skyline.attraction.dao.AttractionFeatureDao;
import com.skyline.attraction.mapper.AttractionFeatureMapper;
import com.skyline.attraction.model.AttractionFeature;
import com.skyline.base.dao.impl.BaseDaoImpl;
import com.skyline.base.type.Activity;
import com.skyline.common.bean.Page;

@Repository("attractionFeatureDao")
public class AttractionFeatureDaoImpl extends BaseDaoImpl implements AttractionFeatureDao {

	/**
	 * insert into attractionfeature(portrait,attractionId,attractionName,
	 * attractionPortrait ,ownerId,ownerNickname
	 * ,ownerPortrait,creatorId,creatorNickname,creatorPortrait
	 * ,title,detail,consume,albumId,createTime,updateTime)
	 * values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,current_timestamp,current_timestamp)
	 */
	@Value("${AttractionFeatureDao.insertFeature}")
	private String insertFeatureSql;
	/**
	 * update attractionfeature set
	 * title=?,detail=?,consume=?,updateTime=current_timestamp where id=?
	 */
	@Value("${AttractionFeatureDao.updateFeature}")
	private String updateFeatureSql;
	/**
	 * update attractionfeature set star =?,votes=votes+1 where id=?
	 */
	@Value("${AttractionFeatureDao.updateStar}")
	private String updateStarSql;
	/**
	 * update attractionfeature set activity=? where id=?
	 */
	@Value("${AttractionFeatureDao.updateActivity}")
	private String updateActivitySql;

	/**
	 * update attractionfeature set portrait=? where id=?
	 */
	@Value("${AttractionFeatureDao.updatePortrait}")
	private String updatePortraitSql;
	/**
	 * select * from attractionfeature where attractionId=? and activity=?
	 */
	@Value("${AttractionFeatureDao.queryFeaturesByAttractionId}")
	private String queryFeaturesByAttractionIdSql;

	/**
	 * select * from attractionfeature where id=? and activity=?
	 */
	@Value("${AttractionFeatureDao.queryFeatureById}")
	private String queryFeatureByIdSql;

	/**
	 * select * from attractionfeature where attractionId=? and activity=? limit
	 * ?
	 */
	@Value("${AttractionFeatureDao.querySomeFeaturesOfAttractionSql}")
	private String querySomeFeaturesOfAttractionSql;

	@Override
	public Long insertFeature(Long attractionId, String attractionName, String attractionPortrait, Long ownerId,
			String ownerNickname, String ownerPortrait, Long creatorId, String creatorNickName, String creatorPortrait,
			String portrait, String title, String detail, Integer consume, Long albumId) {
		return this.insertWithIdReturn(insertFeatureSql, portrait, attractionId, attractionName, attractionPortrait,
				ownerId, ownerNickname, ownerPortrait, creatorId, creatorNickName, creatorPortrait, title, detail,
				consume, albumId);
	}

	@Override
	public void updateFeature(Long id, String title, String detail, Integer consume) {
		jdbcTemplate.update(updateFeatureSql, title, detail, consume, id);

	}

	@Override
	public void updatePortrait(Long id, String portrait) {
		jdbcTemplate.update(updatePortraitSql, portrait, id);
	}

	@Override
	public void updateActivity(Long id, Activity activity) {
		jdbcTemplate.update(updateActivitySql, activity.toString(), id);

	}

	@Override
	public void updateStar(Long id, Float star) {
		jdbcTemplate.update(updateStarSql, star, id);
	}

	@Override
	public List<AttractionFeature> queryFeaturesByAttractionId(Long attractionId, Page page, Activity activity) {
		return this.getPaginationResult(queryFeaturesByAttractionIdSql, page, AttractionFeatureMapper.getMapper(),
				attractionId, activity.toString());
	}

	@Override
	public AttractionFeature queryFeatureById(Long id, Activity activiy) {
		List<AttractionFeature> list = jdbcTemplate.query(queryFeatureByIdSql, AttractionFeatureMapper.getMapper(), id,
				activiy.toString());
		if (CollectionUtils.isEmpty(list))
			return null;
		else
			return list.get(0);
	}

	@Override
	public List<AttractionFeature> querySomeFeaturesOfAttraction(Long attractionId, Activity activity, Integer size) {
		return jdbcTemplate.query(querySomeFeaturesOfAttractionSql, AttractionFeatureMapper.getMapper(), attractionId,
				activity.toString(), size);
	}

}
